package com.deep.spring.v5;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.CacheControl;
import org.springframework.validation.Validator;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

//@EnableWebMvc
@Configuration
public class Test2Conf extends WebMvcConfigurationSupport {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/demo/**").addResourceLocations("classpath:/demo/").setCacheControl(CacheControl.maxAge(-1, TimeUnit.MICROSECONDS));

//        registry.addResourceHandler("/a.txt").addResourceLocations("classpath:/a/a.txt").setCachePeriod(3153600);
        registry.addResourceHandler("/a.txt")
                .addResourceLocations("classpath:/a/a.txt")
                .setCacheControl(CacheControl.maxAge(20, TimeUnit.DAYS).cachePublic())
                .setCachePeriod(14400)
        ;


        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("/static2/**").addResourceLocations("classpath:/static2/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
    }

    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/v5/a").setViewName("a");
    }

//    @Override
//    public void configureViewResolvers(final ViewResolverRegistry registry) {
//        registry.jsp("/templates/", ".jsp");
//    }


    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }

    @Override
    protected void addFormatters(FormatterRegistry registry) {
        // 添加自定义的converter
//        for (Converter<?, ?> converter : getBeansOfType(Converter.class)) {
//            registry.addConverter(converter);
//        }
//        for (GenericConverter converter : getBeansOfType(GenericConverter.class)) {
//            registry.addConverter(converter);
//        }
//        for (Formatter<?> formatter : getBeansOfType(Formatter.class)) {
//            registry.addFormatter(formatter);
//        }
    }

    public <T> List<T> getBeansOfType(Class<T> clazz) {
        List<T> result = new ArrayList<>();
        Map<String, T> map = applicationContext.getBeansOfType(clazz);
        if (null != map) {
            result.addAll(map.values());
        }
        return result;
    }


    @Override
    protected Validator getValidator() {
        // 自定义全局 Validator 实例
        return super.getValidator();
    }

    @Override
    protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        super.configureContentNegotiation(configurer);
    }
}
